package kr.hkit.android_caloriecheck.fragment;

import java.util.ArrayList;

import kr.hkit.android_caloriecheck.DBHelper;
import kr.hkit.android_caloriecheck.R;
import kr.hkit.android_caloriecheck.adapter.DayListAdapter;
import kr.hkit.android_caloriecheck.dao.DAO;
import kr.hkit.android_caloriecheck.dto.MyItem;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class DayFragment extends DialogFragment {
	ListView list;
	ListView list02;
	TextView foodTitle;
	TextView exerciseTitle;

	DBHelper mHelper;
	DAO dao;

	int year;
	int month;
	int day;
	
	public Dialog onCreateDialog(Bundle savedInstanceState) {		
		SharedPreferences pref = getActivity().getSharedPreferences("date", 0);
		year = pref.getInt("year", 0);
		month = pref.getInt("month", 0);
		day = pref.getInt("day", 0);

		Dialog dialog = new Dialog(getActivity());
		dialog.setContentView(R.layout.daylist);
		dialog.setTitle(year + "년 " + month + "월 " + day + "일");
		
		mHelper = new DBHelper(getActivity());
		dao = DAO.open(getActivity());
		
		list = (ListView) dialog.findViewById(R.id.dayList);
		list02 = (ListView) dialog.findViewById(R.id.dayList02);
		foodTitle = (TextView) dialog.findViewById(R.id.foodtitle);
		exerciseTitle = (TextView) dialog.findViewById(R.id.exercisetitle);
		
		int food = dao.foodkcalSumSelectByDate(year, month, day);
		int exercise = dao.exercisekcalSumSelectByDate(year, month, day);
		
		foodTitle.setText("총 섭취량 " + food + "kcal");
		exerciseTitle.setText("총 운동량 " + exercise + "kcal");
		
		ArrayList<MyItem> arList = dao.foodListSelectByDate(year, month, day);
		DayListAdapter adapter = new DayListAdapter(getActivity(), arList,
				R.layout.list02, "food", foodTitle);
		list.setAdapter(adapter);

		ArrayList<MyItem> arList2 = dao.exerciseListSelectByDate(year, month,
				day);
		DayListAdapter adapter2 = new DayListAdapter(getActivity(), arList2,
				R.layout.list02, "exercise", exerciseTitle);
		list02.setAdapter(adapter2);
		
		if(arList.size() == 0 && arList2.size() == 0){
			Toast.makeText(getActivity(), "검색결과 없음", Toast.LENGTH_SHORT).show();
		}
		
		return dialog;
	}
}
